import os
import glob
path_root = "/home/cc/AAA_python_test/out/out_qfl2/"

def combine_files(output_file):
    # 获取所有以out_开头的txt文件，包括子目录
    files = glob.glob(path_root+ "out*.txt", recursive=True)
    
    with open(output_file, 'w') as outfile:
        for fname in files:
            with open(fname) as infile:
                for line in infile:
                    if "iou,score" in line:
                        continue
                    # 替换“[-1],[-1]”为“tensor([-1]),tensor([-1])”
                    line = line.replace("[-1],[-1]", "tensor([-1]),tensor([-1])")
                    outfile.write(line)
                outfile.write("\n")  # 每个文件之间加一个换行符

if __name__ == "__main__":
    output_file = path_root + "combined_output_QFL2.txt"
    combine_files(output_file)
    print(f"所有文件已合并到 {output_file}")